<?php

namespace App\Http\Controllers\Seller;

use App\Models\ShopModelName;
use App\Models\ShopModelValue;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ShopModelController extends BaseController
{
    public function showNames(Request $request)
    {
        return $this->view('seller.shop.showmodelname');
    }

    public function showValues(Request $request)
    {
        $model_id = $request->input('model_id', 0);
        return $this->view('seller.shop.showmodelvalue', compact('model_id'));
    }

    public function getNames(Request $request)
    {
        $items = $this->shop->itemModels;
        return ajaxReturn(['items'=>$items]);
    }

    public function getValues(Request $request)
    {
        $model_id = $request->input('model_id', 0);
        $items = ShopModelValue::where('model_id', $model_id)->get();
        return ajaxReturn(['items'=>$items]);
    }

    public function saveName(Request $request)
    {
        $model_name = $request->input('model_name', '');
        if ($model_name)
        {
            $model = $this->shop->itemModels()->where('model_name', $model_name)->firstOrCreate(['model_name'=>$model_name]);
            return ajaxReturn(['model'=>$model]);
        }
        return ajaxError(1, '');
    }

    public function saveValue(Request $request)
    {
        $model_id = $request->input('model_id');
        $model_value = $request->input('model_value');

        if ($model_value)
        {
            $value = ShopModelValue::where('model_id', $model_id)
                ->where('model_value', $model_value)
                ->firstOrCreate([
                    'model_id'=>$model_id,
                    'model_value'=>$model_value
                ]);

            return ajaxReturn(['value'=>$value]);
        }
        return ajaxError(1, '');
    }
}
